from common.baserequest import Base_request


class EsUtils:
    br = Base_request()

    def get_data(self, ip, index, sql, user, pwd):

        url_start = "http://" + ip + "/" + index + "/_search?scroll=1m"
        url_end = "http://" + ip + "/_search/scroll"
        es_check_listing = []
        res = self.br.post_auth(url=url_start, data=sql,
                                user=user, pwd=pwd)
        # ("2023-10-24 12:00:00","2023-10-25 16:00:00")
        if len(res['hits']['hits']) != 0 and len(res)!=0:
            data_list = list(map(lambda x: x['_source'], res['hits']['hits']))
            es_check_listing += data_list
        else:
            return []
        _scroll_id = res["_scroll_id"]
        data2 = {
            "scroll": "1m",
            "scroll_id": _scroll_id
        }

        while True:
            res2 = self.br.post_auth(url=url_end, data=data2,
                                     user=user, pwd=pwd)
            # if res2:
            if len(res2['hits']['hits']) != 0:
                data_list = list(map(lambda x: x['_source'], res2['hits']['hits']))

                es_check_listing += data_list
            else:
                break
        print("检查的listing数目{}个".format(len(es_check_listing)))
        return es_check_listing


if __name__ == '__main__':
    sql = {
        "query": {
            "bool": {
                "should": [
                    {
                        "match": {
                            "Brand": "J.A.N-Green Life"
                        }
                    },
                    {
                        "match": {
                            "Brand": "LuctaCaps Pure"
                        }
                    },
                    {
                        "match": {
                            "Brand": "COSYLAND"
                        }
                    },
                    {
                        "match": {
                            "Brand": "LA FORÊT RÊVE"
                        }
                    },
                    {
                        "match": {
                            "Brand": "VSSHE"
                        }
                    },
                    {
                        "match": {
                            "Brand": "NYMPHÉES MERVEILLES"
                        }
                    },
                    {
                        "match": {
                            "Brand": "BOTABOX"
                        }
                    },
                    {
                        "match": {
                            "Brand": "LA VALLÉE DU TEMPS"
                        }
                    },
                    {
                        "match": {
                            "Brand": "VITATONE"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Joh. Springer’s Erben"
                        }
                    },
                    {
                        "match": {
                            "Brand": "SEFA"
                        }
                    },
                    {
                        "match": {
                            "Brand": "SES Technologies"
                        }
                    },
                    {
                        "match": {
                            "Brand": "THE SMILE FACTORY"
                        }
                    },
                    {
                        "match": {
                            "Brand": "3CE FUTURE KIND"
                        }
                    },
                    {
                        "match": {
                            "Brand": "TEMPLE DES MOUSSES"
                        }
                    },
                    {
                        "match": {
                            "Brand": "TERRES BLONDES"
                        }
                    },
                    {
                        "match": {
                            "Brand": "STEP FILTERS"
                        }
                    },
                    {
                        "match": {
                            "Brand": "IGIASI S.A."
                        }
                    },
                    {
                        "match": {
                            "Brand": "CARDIO SWITCH"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Rolevin"
                        }
                    },
                    {
                        "match": {
                            "Brand": "G"
                        }
                    },
                    {
                        "match": {
                            "Brand": "XNJIHWA"
                        }
                    },
                    {
                        "match": {
                            "Brand": "silana"
                        }
                    },
                    {
                        "match": {
                            "Brand": "ProID"
                        }
                    },
                    {
                        "match": {
                            "Brand": "RS Rea Sevt"
                        }
                    },
                    {
                        "match": {
                            "Brand": "SPIRUAL"
                        }
                    },
                    {
                        "match": {
                            "Brand": "ANCHOR FLUID POWER"
                        }
                    },
                    {
                        "match": {
                            "Brand": "UNE VIE CHANVRISSIME"
                        }
                    },
                    {
                        "match": {
                            "Brand": "TONINI"
                        }
                    },
                    {
                        "match": {
                            "Brand": "DIVA FLORA"
                        }
                    },
                    {
                        "match": {
                            "Brand": "CLARINS PRECIOUS Rare. Tout comme vous"
                        }
                    },
                    {
                        "match": {
                            "Brand": "XUIIOSOOKP"
                        }
                    },
                    {
                        "match": {
                            "Brand": "QUIIEOSKIA"
                        }
                    },
                    {
                        "match": {
                            "Brand": "HNPANZE"
                        }
                    },
                    {
                        "match": {
                            "Brand": "buvyuotu"
                        }
                    },
                    {
                        "match": {
                            "Brand": "GENTLE LAY"
                        }
                    },
                    {
                        "match": {
                            "Brand": "HIP CHIC"
                        }
                    },
                    {
                        "match": {
                            "Brand": "NATERNAL"
                        }
                    },
                    {
                        "match": {
                            "Brand": "LIA VIE"
                        }
                    },
                    {
                        "match": {
                            "Brand": "AQUAFLATE"
                        }
                    },
                    {
                        "match": {
                            "Brand": "LKHQ LOW KEY HIGH QUALITY"
                        }
                    },
                    {
                        "match": {
                            "Brand": "BeeHarmony"
                        }
                    },
                    {
                        "match": {
                            "Brand": "RoyalBalance"
                        }
                    },
                    {
                        "match": {
                            "Brand": "KOHEN"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Osmundson"
                        }
                    },
                    {
                        "match": {
                            "Brand": "AMINO DYAL"
                        }
                    },
                    {
                        "match": {
                            "Brand": "GREEN BREAKING"
                        }
                    },
                    {
                        "match": {
                            "Brand": "YOLO products"
                        }
                    },
                    {
                        "match": {
                            "Brand": "NEURVATI NEUROSCIENCES"
                        }
                    },
                    {
                        "match": {
                            "Brand": "NOCUSOL"
                        }
                    },
                    {
                        "match": {
                            "Brand": "etiMed"
                        }
                    },
                    {
                        "match": {
                            "Brand": "QUITE A BORING INGREDIENT"
                        }
                    },
                    {
                        "match": {
                            "Brand": "ELASTOPLAST SECOND SKIN PROTECTION"
                        }
                    },
                    {
                        "match": {
                            "Brand": "HANSAPLAST SECOND SKIN PROTECTION"
                        }
                    },
                    {
                        "match": {
                            "Brand": "RoyalYouth"
                        }
                    },
                    {
                        "match": {
                            "Brand": "PANAUDENZ"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Multi C"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Kuhx - die Kapsel mit dem Bauernhof-Effekt"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Ostseegold"
                        }
                    },
                    {
                        "match": {
                            "Brand": "SNOWO"
                        }
                    },
                    {
                        "match": {
                            "Brand": "[gǝ'dø:ns]"
                        }
                    },
                    {
                        "match": {
                            "Brand": "GlideHype"
                        }
                    },
                    {
                        "match": {
                            "Brand": "neuro charge"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Red Crane MINIATURES"
                        }
                    },
                    {
                        "match": {
                            "Brand": "RÜblanc"
                        }
                    },
                    {
                        "match": {
                            "Brand": "RÜsé"
                        }
                    },
                    {
                        "match": {
                            "Brand": "MarLie Ihr regionaler IHK geprüfter Schädlingsbekämpfer"
                        }
                    },
                    {
                        "match": {
                            "Brand": "TISCHLING"
                        }
                    },
                    {
                        "match": {
                            "Brand": "RHEINGLÜCK"
                        }
                    },
                    {
                        "match": {
                            "Brand": "THE LASER DERM LASER & BEAUTY CENTER"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Vogel Nature"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Parae"
                        }
                    },
                    {
                        "match": {
                            "Brand": "d-selection"
                        }
                    },
                    {
                        "match": {
                            "Brand": "mgverkauf"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Art & DESIGN Tegernsee"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Guide Fishman"
                        }
                    },
                    {
                        "match": {
                            "Brand": "FANTASIA TOBACCO"
                        }
                    },
                    {
                        "match": {
                            "Brand": "GK Industry Advisors"
                        }
                    },
                    {
                        "match": {
                            "Brand": "WeinFreundschaft"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Ada"
                        }
                    },
                    {
                        "match": {
                            "Brand": "PARAXITE"
                        }
                    },
                    {
                        "match": {
                            "Brand": "GOIYZTTR"
                        }
                    },
                    {
                        "match": {
                            "Brand": "AZED"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Mastlabs"
                        }
                    },
                    {
                        "match": {
                            "Brand": "ΥΓΕΙΑΣΗ Α.Ε."
                        }
                    },
                    {
                        "match": {
                            "Brand": "COGNITIVE SWITCH"
                        }
                    },
                    {
                        "match": {
                            "Brand": "FLEXICOLOR"
                        }
                    },
                    {
                        "match": {
                            "Brand": "FUTURE RENEW"
                        }
                    },
                    {
                        "match": {
                            "Brand": "SFT POLYMER"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Zambernardi"
                        }
                    },
                    {
                        "match": {
                            "Brand": "LEJDYVITA"
                        }
                    },
                    {
                        "match": {
                            "Brand": "WHAT’S COOKING?"
                        }
                    },
                    {
                        "match": {
                            "Brand": "Period Pack"
                        }
                    },
                    {
                        "match": {
                            "Brand": "APPS"
                        }
                    },
                    {
                        "match": {
                            "Brand": "INSIGHT"
                        }
                    }
                ]
            }
        }
    }

    sql1 = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "UpdateTime": {
                                    "from": "2023-06-01T12:00:00.0000000+08:00",
                                    "to": "2023-12-20T00:00:00.0000000+08:00"
                                }
                            }
                        }
                    ]
                }
            },
            "size": 10000
        }
    # EsUtils().get_data("139.159.245.112:9202", "sensitive_wipo_distinctbrand_v2", sql, "appuser",
    #                    "B4eE7Az503q8Fk6yg6sL")
    EsUtils().get_data("139.159.245.112:9204", "kogan_listing", sql1, "reader",
                       "UCkeB5cmV5oGwFPl#")
